package com.wlw.admin.openeye.ui.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.v4.app.ActivityCompat
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.util.Pair
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.hazz.kotlinmvp.view.recyclerview.ViewHolder
import com.hazz.kotlinmvp.view.recyclerview.adapter.CommonAdapter
import com.wlw.admin.openeye.Constants
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.durationFormat
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.ui.activity.VideoDetailActivity

class CategoryDetailAdapter(context: Context, dataList: ArrayList<HomeBean.Issue.Item>) : CommonAdapter<HomeBean.Issue.Item>(context, dataList, R.layout.item_category_detail) {
    fun addData(dataList: ArrayList<HomeBean.Issue.Item>) {
        mData = dataList
        notifyDataSetChanged()
    }


    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        setVideoItem(holder, data)
    }

    private fun setVideoItem(holder: ViewHolder, item: HomeBean.Issue.Item) {
        val itemData = item.data
        val cover = itemData?.cover?.feed ?: ""
        Glide.with(mContext)
                .load(cover)
                .apply(RequestOptions().placeholder(R.mipmap.placeholder_banner))
                .transition(DrawableTransitionOptions().crossFade())
                .into(holder.getView(R.id.iv_image))
        holder.setText(R.id.tv_title, itemData?.title ?: "")
        val timeFormat = durationFormat(itemData?.duration)
        holder.setText(R.id.tv_tag, "#${itemData?.category}/$timeFormat")
        holder.itemView.setOnClickListener {
            goToVideoPlayer(mContext as Activity, holder.getView<ImageView>(R.id.iv_image), item)
        }
    }

    private fun goToVideoPlayer(activity: Activity, view: ImageView, item: HomeBean.Issue.Item) {
        val intent = Intent(activity, VideoDetailActivity::class.java)
        intent.putExtra(Constants.BUNDLE_VIDEO_DATA, item)
        intent.putExtra(VideoDetailActivity.IMG_TRANSITION, true)
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            val pair = Pair<View, String>(view, VideoDetailActivity.IMG_TRANSITION)
            val activityOptionCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pair)
            ActivityCompat.startActivity(activity, intent, activityOptionCompat.toBundle())

        } else {
            activity.startActivity(intent)
            activity.overridePendingTransition(R.anim.anim_in, R.anim.anim_out)
        }
    }
}